package com.luck.easyfoodbatch.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.luck.easyfoodbatch.entity.City;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lmw
 * @ClassName: FindDB
 * @Description: 数据库查询类
 * @date 2014-12-2 下午1:42:15
 */
public class FindDB {
    private static SQLiteDatabase database;
    private DBManager dBManager;

    public FindDB(Context context) {
        dBManager = new DBManager(context);

    }

    /**
     * @return void 返回类型
     * @Title: openDataBase
     * @Description: 打开数据库
     * @author lmw
     */
    protected void openDataBase() {
        database = dBManager.openDatabase();
    }

    /**
     * @return void 返回类型
     * @Title: openDataBase
     * @Description: 关闭数据库
     * @author lmw
     */
    protected void closeDataBase() {
        if (null != database) {
            database.close();
        }
    }

    /**
     * 根据省名查询相应的id
     */
    public String getProvinceById(String province) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        Cursor cur = database
                .rawQuery("SELECT * from city where name='" + province + "'", null);
        try {
            if (cur != null) {
                int num = cur.getCount();
                String province_name = "";
                if (cur.moveToFirst()) {
                    do {
                        province_name = cur.getString(cur
                                .getColumnIndex("id"));
                        return province_name;
                    } while (cur.moveToNext());
                }
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            cur.close();
        }
        return "";
    }


    /**
     * @return ArrayList<Sort> 返回类型
     * @Title: getProvinces
     * @Description: 返回所有省份
     * @author lmw
     */
    public List<City> getProvinces() {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        Cursor cur = database
                .rawQuery("SELECT * from city where pid=0", null);
        try {
            if (cur != null) {
                int num = cur.getCount();
                List<City> sorts = new ArrayList<City>(num);
                if (cur.moveToFirst()) {
                    do {
                        String name = cur.getString(cur
                                .getColumnIndex("name"));
                        String id = cur.getString(cur.getColumnIndex("id"));
                        City sort = new City();
                        sort.setId(id);
                        sort.setName(name);
                        sorts.add(sort);
                    } while (cur.moveToNext());
                }
                return sorts;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            cur.close();
        }
        return null;
    }

    /**
     * @param
     * @return ArrayList<Model> 返回类型
     * @Title: getCitys
     * @Description: 返回对应省份的所有城市
     * @author lmw
     */
    public List<City> getCitys(String sortId) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        Cursor cur = database.rawQuery("SELECT * from city where pid ='"
                + sortId + "'", null);
        try {

            if (cur != null) {
                int num = cur.getCount();
                List<City> citys = new ArrayList<City>(num);
                if (cur.moveToFirst()) {
                    do {
                        String name = cur.getString(cur
                                .getColumnIndex("name"));
                        String id = cur.getString(cur.getColumnIndex("id"));
                        City city = new City();
                        city.setId(id);
                        city.setName(name);
                        citys.add(city);
                    } while (cur.moveToNext());
                }

                return citys;
            }
        } catch (Exception e) {

        } finally {
            cur.close();
        }
        return null;
    }


    /**
     * @param
     * @return ArrayList<Model> 返回类型
     * @Title: getCitys
     * @Description: 返回对应省份的所有城市的区
     * @author lmw
     */
    public List<City> getAreas(String sortId) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
//        switch (Integer.parseInt(sortId)) {
//            case 1:
//                sortId = "36";
//                break;
//            case 2:
//                sortId = "37";
//                break;
//            case 3:
//                sortId = "38";
//                break;
//            case 4:
//                sortId = "39";
//                break;
//
//        }
        Cursor cur = database.rawQuery("SELECT * from city where pid ='"
                + sortId + "'", null);
        try {

            if (cur != null) {
                int num = cur.getCount();
                List<City> citys = new ArrayList<City>(num);
                if (cur.moveToFirst()) {
                    do {
                        String name = cur.getString(cur
                                .getColumnIndex("name"));
                        String id = cur.getString(cur.getColumnIndex("id"));
                        City city = new City();
                        city.setId(id);
                        city.setName(name);
                        citys.add(city);
                    } while (cur.moveToNext());
                }

                return citys;
            }
        } catch (Exception e) {

        } finally {
            cur.close();
        }
        return null;
    }


    /**
     * @return void 返回类型
     * @Title: deleteDataBase
     * @Description: 删除数据库
     * @author lmw
     */
    public void deleteDataBase(Context context) {
        context.deleteDatabase("area.db");
    }
}
